<?php if(count($this->errors) > 0){
     
    ?>

<div class="alert alert-danger alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <?php 
                    foreach ($this->errors as $errorName => $error){
                        foreach ($error as $message){
                                        echo "<strong>$errorName : </strong>$message<br />";
                        }
                    }
                                        ?>
</div>
<?php } ?>
<div class="panel panel-primary">
    <div class="panel-heading">
    <h3 class="panel-title"><?php echo $this->viewName?></h3>
  </div>
<div class="panel-body">
<form action="<?php echo $this->baseUrl().$this->post?>" method="post">
  <div class="form-group">
    <label for="roleName">Role Name</label>
    <?php echo $this->form->id ?>
    <?php echo $this->form->name ?>
    </div>
     <div class="pull-right">
    <label id="selectAll"><input type="checkbox" class="selectAll" /> Select All</label>
    </div>
    <div class="clearfix"></div>
    <div class="form-group">
       
    <?php
    $moduleArray = array();
    $resourceArray = array();
    foreach ($this->resources as $resource){
                    if(!in_array($resource['module'], $moduleArray)){
                            $moduleArray[] = $resource['module'];
                    }
                 }
        
        foreach ($moduleArray as $module){
            foreach ($this->resources as $resource){
                if($module === $resource['module']){
                    if(! @in_array($resource['controller'], $resourceArray[$module])){
                        $resourceArray[$module][]=$resource['controller'];
                    }
                 }
            }
        }
        
        
    foreach ($resourceArray as $resourcKey => $resourceValue){
        ?>
            <div class="panel-group" id="accordion">
              <div class="panel panel-default">
                <div class="panel-heading">
                  <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#<?= $resourcKey?>">
                       <?php echo ucfirst($resourcKey); ?>
                    </a>
                  </h4>
                </div>
                  <div id="<?= $resourcKey?>" class="panel-collapse collapse in">
                  
    <?php
            foreach ($resourceValue as $controller){
                
                echo '<div class="panel-body">';
                echo '<div class="row">';
                echo '<div class="col-md-2">';
                echo "<strong>".ucfirst($controller)."</strong>";
                echo '</div>';
                echo '<div class="col-md-8">';
                 foreach ($this->resources as $resource){
                     if($resource['module'] === $resourcKey && $resource['controller'] === $controller){
                         echo '<label class="checkbox-inline">';
                         $options = array();
                         if($this->rolePermissions){
                             foreach ($this->rolePermissions as $permission){
                                     if(in_array($resource['id'], $permission)){
                                         $options = array('checked' => true);
                                     } 
                                 }
                         }
                         echo $this->formCheckbox('resources[]',$resource['id'],$options).$resource['action'];
                         echo '</label>';
                      }
                 }
                 echo '</div>'
                 . '<div class="col-md-2"><label class="checkbox-inline"><input type="checkbox" class="selectAll" /> Select All</label></div>'
            . '</div>'
                    . '</div>';
            }
            echo '</div>'
            . '</div>'
                    . '</div>';
    }
    
            ?>
            
    <?php echo $this->form->submit ?>
</form>
    </div>
</div>
</div>
</div>